package com.platform.destiny.config;

import com.google.common.collect.Lists;
import com.platform.destiny.dto.UserDto;
import com.platform.destiny.entity.Role;
import com.platform.destiny.entity.User;
import com.platform.destiny.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户查询
 */
@Slf4j
@Service
public class AuthService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userMapper.queryUserByName(username);
        UserDto dto = new UserDto();
        dto.setId(user.getId());
        dto.setUsername(user.getUsername());
        dto.setPassword(user.getPassword());
        dto.setEnabled(true);
        dto.setAccountNonExpired(true);
        dto.setAccountNonLocked(true);
        dto.setCredentialsNonExpired(true);
        // 角色
        dto.setRoles(Lists.newArrayList(new Role("admin"), new Role("user")));
        // 权限
        dto.setPermissions(Lists.newArrayList("app:user:detail", "app:user:del", "app:user:page", "app:user:add", "app:user:update"));

        return dto;
    }


}
